#------------------------------------------------------------------------------
# Ratio of Spreads: Baseline vs Perfect Information
#------------------------------------------------------------------------------

using Interpolations, Optim, Roots, JLD, Plots
using Distributions, StatsBase

include("../02_Perfect_Information/01_Model_Structure.jl")
include("../01_Baseline_Model/01_Model_Structure.jl");
include("../01_Baseline_Model/02_Functions.jl");
include("../01_Baseline_Model/03_Model.jl")
include("../01_Baseline_Model/04_Main_Iteration_fx.jl");
include("../01_Baseline_Model/05_Load_Solution.jl");
include("../01_Baseline_Model/10_Simulations_Main_fx.jl");


#------------------------------------------------------------------------------
# LOAD PERFECT INFO CASE
#------------------------------------------------------------------------------
name_file   = string("../02_Perfect_Information/model_data/CE_model_PerfInfo.jld");
ce_PI  		= load(string(name_file), "ce")
q_PI   		= ce_PI.q_mat+0.;


#------------------------------------------------------------------------------
# LOAD BASELINE MODEL & SIMULATE
#------------------------------------------------------------------------------
name_file  = string("../01_Baseline_Model/model_data/CE_model_structure_alpha_",0.028,".jld");
ce         = load(string(name_file), "ce");
q_Baseline = ce.q_mat+0.;

# Simulate just to get moments of b and y
T=500_000
N=1
y_sim, type_sim, b_sim, π_sim, def_sim, def_pol, def_ind, q_sim, SP_sim, SP_HR_sim,c_sim, tb_sim, m_sim, m_sim_ctfl, ζ_sim, dlnSP_sim,  dBE_sim, b_sim_tmpI, inf_bp  =
simul_econ(ce::CE_Economy_M, T,N; T0=2 )
b_baseline = [b_sim; b_sim[end]; b_sim[end]]


#-----------------------------------------------------------------------------
# PLOT PRICING KERNEL FOR A FIXED LEVEL OF DEBT AND Y
#-----------------------------------------------------------------------------
#Interpolate spreads
Spreads_Baseline     = Spreads_fx_matrix(ce,q_Baseline[:,:,:])
knots 				 = (ce.b_grid, ce.y_grid, ce.z_grid)
FF_Spreads_Baseline  = interpolate(knots, Spreads_Baseline, Gridded(Linear()));
Spreads_PI           = Spreads_fx_matrix(ce,q_PI[:,:,:])
knots                = (ce_PI.b_grid, ce_PI.y_grid, 1:1:2)
FF_Spreads_PI        = interpolate(knots, Spreads_PI, Gridded(Linear()));

# PLOT
type_id =1 #C-type

# Choose debt, output and reputation
filter_sp = (def_sim.==0).*(type_sim.==type_id)

b_m =     mean(b_sim[filter_sp])
b_l =b_m- std(b_sim[filter_sp])*0.5
b_u =b_m+ std(b_sim[filter_sp])*0.5

y_m =     mean(y_sim[filter_sp])
y_l =y_m- std(y_sim[filter_sp])*0.5
y_h =y_m+ std(y_sim[filter_sp])*0.5

ζ_m = mean(ζ_sim[filter_sp])
ζ_h = percentile(ζ_sim[filter_sp],75)
ζ_l = percentile(ζ_sim[filter_sp],25)

int = 0.00005
ratio_m    = [FF_Spreads_Baseline[b_l:int:b_u,y_m,ζ_m][:]./FF_Spreads_PI[b_l:int:b_u,y_m,type_id][:]]
ratio_m_ζh = [FF_Spreads_Baseline[b_l:int:b_u,y_m,ζ_h][:]./FF_Spreads_PI[b_l:int:b_u,y_m,type_id][:]]
ratio_m_ζl = [FF_Spreads_Baseline[b_l:int:b_u,y_m,ζ_l][:]./FF_Spreads_PI[b_l:int:b_u,y_m,type_id][:]]

ratio_l    = [FF_Spreads_Baseline[b_l:int:b_u,y_l,ζ_m][:]./FF_Spreads_PI[b_l:int:b_u,y_l,type_id][:]]
ratio_l_ζh = [FF_Spreads_Baseline[b_l:int:b_u,y_l,ζ_h][:]./FF_Spreads_PI[b_l:int:b_u,y_l,type_id][:]]
ratio_l_ζl = [FF_Spreads_Baseline[b_l:int:b_u,y_l,ζ_l][:]./FF_Spreads_PI[b_l:int:b_u,y_l,type_id][:]]

ratio_h    = [FF_Spreads_Baseline[b_l:int:b_u,y_h,ζ_m][:]./FF_Spreads_PI[b_l:int:b_u,y_h,type_id][:]]
ratio_h_ζh = [FF_Spreads_Baseline[b_l:int:b_u,y_h,ζ_h][:]./FF_Spreads_PI[b_l:int:b_u,y_h,type_id][:]]
ratio_h_ζl = [FF_Spreads_Baseline[b_l:int:b_u,y_h,ζ_l][:]./FF_Spreads_PI[b_l:int:b_u,y_h,type_id][:]]

plot((b_l:int:b_u),    ratio_l  ,  label="Low y",   c="red",   linestyle=:solid )
plot!((b_l:int:b_u),   ratio_m ,   label="Mean y",  c="black",  linestyle=:solid )
plot!((b_l:int:b_u),   ratio_h  ,  label="High y",  c="blue",  linestyle=:solid )
plot!((b_l:int:b_u),   ratio_l_ζl, fillrange= ratio_l_ζh, label="", c="red", linestyle=:solid, fillalpha=0.1, alpha=0.1 )
plot!((b_l:int:b_u),   ratio_m_ζl, fillrange= ratio_m_ζh, label="", c="black", linestyle=:solid, fillalpha=0.1, alpha=0.1 )
plot!((b_l:int:b_u),   ratio_h_ζl, fillrange= ratio_h_ζh, label="", c="blue", linestyle=:solid, fillalpha=0.1, alpha=0.1 )
xlabel!("b")
vline!([mean(b_sim[filter_sp])], c="black", linestyle=:dot,label="")
ylabel!("Ratio of Spreads", legend=:topleft, ylim=[1.,1.6])
xlims!(b_l,b_u)
ylims!(1,1.6)
savefig("figures/Ratio_Spreads_Baseline_vs_PerfInfo.pdf")
